/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.web.servlet.handler.bluemartini;


import javax.servlet.ServletException;

import com.bluemartini.dna.BMContext;
import com.bluemartini.dna.BMThreadManager;
import com.bluemartini.util.HTMLVersionUtil;

/**
 * Provides a thread manager handler for a 
 * Blue Martini Web Connect server environment.
 * 
 * @author Scott Rossillo
 *
 */
public final class WebConnectThreadManagerHandler extends AbstractBlueMartiniTheadManagerHandler {
	
	/**
	 * Creates a new web connect thread manager handler.
	 */
	public WebConnectThreadManagerHandler() { }

	/* (non-Javadoc)
	 * @see net.martinimix.web.servlet.handler.bluemartini.AbstractBlueMartiniTheadManagerHandler#setThreadVersion()
	 */
	protected void setThreadVersion() throws ServletException {
		try {
			HTMLVersionUtil.getDefaultVersion();
		} catch (Throwable t) {
			String env = BMContext.getAppConfig().getString("using_env");
			Integer version;
			try {
				version = BMContext.getVersionIDForEnv(env);
				HTMLVersionUtil.getVersion(version);
				BMThreadManager.setCurrentVersionID(version);
			} catch (Throwable t2) {
				log.error("Unable to set thread version");
				throw new ServletException("Unable to set thread version: " + t2.getMessage(), t2);
			}
		}
	}

}
